<?php
  require_once('db_connection.php');

if(isset($_SESSION['name'])) {
  redirect('show_all_users.php');
}

  //vx($_POST);


  # For Login
  if(isset($_POST['email']))
  {
    $_POST = array_map('trim',$_POST);
    $_POST = array_map('mysql_real_escape_string',$_POST);

    //vx($_POST);
    $email = $_POST['email'];
    $password = md5($_POST['password']);

    $user = fetchOne('users',"`email`='$email' AND `password`='$password' AND `status`='1'");
//pr($user);
//exit;
    if($user==false){
      echo error("Email / Password Incorrect.");
    } else {

//pr($_POST);
if(isset($_POST['remember']))
{
      $time = 60*60*24*30;
      //$time = 30;
      setcookie('email',$_POST['email'],time()+$time);
      setcookie('password',$_POST['password'],time()+$time);
} else {
      setcookie('email','',time()-1);
      setcookie('password','',time()-1);
}
      $_SESSION['name'] = $user['name'];
      $_SESSION['email'] = $user['email'];
      $_SESSION['added_date'] = $user['added_date'];
      redirect('show_all_users.php');
    }

//vd($user);
  }
  //pr1($_COOKIE);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="bootstrap/assets/ico/favicon.png">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="bootstrap/dist/css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="signin.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="bootstrap/assets/js/html5shiv.js"></script>
      <script src="bootstrap/assets/js/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <div class="container">

      <form class="form-signin" method="post">
        <h2 class="form-signin-heading">Please sign in</h2>
        <input type="email" value="<?=@$_COOKIE['email']?>" required name="email" class="form-control" placeholder="Email address" autofocus>
        <input type="password" required  name="password" class="form-control" value="<?=@$_COOKIE['password']?>" placeholder="Password">
        <label class="checkbox">
          <input name="remember" checked type="checkbox"> Remember me
        </label>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p style="margin-top: 10px;">Didn't account yet?<a href="add_user.php">Register</a>
</p>
      </form>

    </div> <!-- /container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
  </body>
</html>
